import java.awt.Component;
import java.awt.Rectangle;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.media.protocol.DataSource;
import javax.tv.xlet.Xlet;
import javax.tv.xlet.XletContext;
import javax.tv.xlet.XletStateChangeException;

import org.havi.ui.HContainer;
import org.havi.ui.HScene;
import org.havi.ui.HSceneFactory;
import org.havi.ui.HSceneTemplate;

public class VideoXletView extends HContainer implements Xlet, ControllerListener {

    private static final String VIDEO_URL = "file://f://tcc//meuvideo.avi";

    private HScene scene;

    private XletContext context;

    private Player player;

    public void initXlet(XletContext ctx) throws XletStateChangeException {
        this.context = ctx;

        HSceneFactory factory = HSceneFactory.getInstance();

        HSceneTemplate hst = new HSceneTemplate();
        hst.setPreference(HSceneTemplate.SCENE_SCREEN_DIMENSION, new org.havi.ui.HScreenDimension(1, 1), HSceneTemplate.REQUIRED);
        hst.setPreference(HSceneTemplate.SCENE_SCREEN_LOCATION, new org.havi.ui.HScreenPoint(0, 0), HSceneTemplate.REQUIRED);

        scene = factory.getBestScene(hst);

        Rectangle rect = scene.getBounds();
        setBounds(rect);

        scene.setVisible(false);
        scene.add(this);
    }

    public void startXlet() {

        javax.media.MediaLocator locator;

        while (true) {
            try {
                locator = new javax.media.MediaLocator(VIDEO_URL);
                DataSource dataSource = Manager.createDataSource(locator);
                player = Manager.createPlayer(dataSource);

                player.addControllerListener(this);

                player.start();

            } catch (Exception e) {
                e.printStackTrace();
            }
            scene.setVisible(true);
        }

    }

    public void pauseXlet() {}

    public void destroyXlet(boolean unconditional) {
        player.stop();
        player.close();
        player = null;
        if (scene != null) {
            scene.setVisible(false);
            scene.remove(this);
            HSceneFactory.getInstance().dispose(scene);
        }
        scene = null;
    }

    public void controllerUpdate(ControllerEvent event) {
        if (event instanceof RealizeCompleteEvent) {
            if (player.getVisualComponent() != null) {
                Component component = player.getVisualComponent();
                add(component);
                component.setSize(720, 576);
                setSize(720, 576);
                validate();
                repaint();
            }
        } else if (event instanceof EndOfMediaEvent) {
            player.setMediaTime(new Time(0));
            player.start();
        }
    }
}